package com.buba.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import entity.device.PlaceForStorage;
import entity.maintenance.MaintenanceProgram;
import com.buba.mapper.MaintenanceProgramMapper;
import com.buba.service.MaintenanceProgramService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import entity.maintenance.dto.MaintenanceProgramDto;
import entity.maintenance.dto.MaintenanceProgramQueryWrapperDto;
import entity.system.CodeRules;
import feign.systemFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import utils.pageutil.MyPageHelper;
import utils.pageutil.PageRequest;
import utils.pageutil.PageResult;


import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 保养项目 服务实现类
 * </p>
 *
 * @author beisheng
 * @since 2023-05-18
 */
@Service
public class MaintenanceProgramServiceImpl extends ServiceImpl<MaintenanceProgramMapper, MaintenanceProgram> implements MaintenanceProgramService {

    @Autowired
    private MaintenanceProgramMapper maintenanceProgramMapper;

    @Autowired
    private feign.systemFeign systemFeign;

    /**
     * 分页模糊全查保养项目列表
     *
     * @param maintenanceProgramDto
     * @return
     */
    @Override
    public PageResult selMaintenanceProgram(MaintenanceProgramDto maintenanceProgramDto) {
        MaintenanceProgramQueryWrapperDto queryWrapperDto = new MaintenanceProgramQueryWrapperDto();
        QueryWrapper<MaintenanceProgram> conditional = queryWrapperDto.conditional(maintenanceProgramDto);
        PageRequest pageRequest = new PageRequest(maintenanceProgramDto.getPage(), maintenanceProgramDto.getPageSize());
        MyPageHelper.startPage(pageRequest);
        List<MaintenanceProgram> list = maintenanceProgramMapper.selectList(conditional);
        PageResult pageResult = MyPageHelper.getPageResult(list);
        return pageResult;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public boolean delMaintenanceProgram(List<Integer> ids) {
        int i = maintenanceProgramMapper.deleteBatchIds(ids);
        return i > 0;
    }

    /**
     * 新增保养项目
     *
     * @param maintenanceProgram
     * @return
     */
    @Override
    public boolean insMaintenanceProgram(MaintenanceProgram maintenanceProgram) {
        //根据规则生成编号
        CodeRules byxm = systemFeign.selAllRules("BYXM");
        //查询时间格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(byxm.getDateTime());
        //拼接所需时间字符串
        String format = simpleDateFormat.format(new Date());
        //当前表长度
        int size = maintenanceProgramMapper.selectList(new QueryWrapper<MaintenanceProgram>().eq("delete_flag", 0)).size();
        //生成编码
        String code = byxm.getPrefix() + format + "0" + (size + 1);

        maintenanceProgram.setProgramCoding(code);
        maintenanceProgram.setOperator("张三");
        maintenanceProgram.setCreateTime(new Date());
        int insert = maintenanceProgramMapper.insert(maintenanceProgram);
        return insert > 0;
    }

    /**
     * 编辑保养项目
     *
     * @param maintenanceProgram
     * @return
     */
    @Override
    public boolean updateMaintenanceProgram(MaintenanceProgram maintenanceProgram) {
        maintenanceProgram.setUpdateTime(new Date());
        UpdateWrapper<MaintenanceProgram> UpdateWrapper = new UpdateWrapper<>();
        UpdateWrapper.eq("program_id", maintenanceProgram.getProgramId());
        UpdateWrapper.eq("delete_flag",0);
        int update = maintenanceProgramMapper.update(maintenanceProgram, UpdateWrapper);
        return update > 0;
    }
}
